Added some tests and simple optimization for collision algorithm.
[brmtron.git] / BrmTron Server / src / cz / brmlab / brmtron / server / test / BrmTronServerTest.java
bloba2ec90f6e37adff7d98d21b777736e97260de590
1 package cz.brmlab.brmtron.server.test;
3 import static org.junit.Assert.*;
5 import java.util.Random;
7 import org.junit.Test;
9 import cz.brmlab.brmtron.server.BrmTronServer;
10 import cz.brmlab.brmtron.server.PathElement;
12 public class BrmTronServerTest extends BrmTronServer{
14 @Test
15 public void test_calcIntersection() {
16 final double dx = 1e-09;
18 double[] start1 = {0.0, 0.0};
19 double[] end1 = {0.0, 2.0};
20 double[] start2 = {-1.0, 0.0};
21 double[] end2 = {1.0, 2.0};
23 double[] res = {0.0, 1.0};
25 PathElement e1 = new PathElement(start1, end1);
26 PathElement e2 = new PathElement(start2, end2);
28 double[] brm = BrmTronServer.calcIntersection(e1, e2);
30 assertTrue(compareDouble(brm, res, dx));
33 @Test
34 public void test1_findIntersection() {
35 final double dx = 1e-09;
37 double[] start1 = {0.0, 0.0};
38 double[] end1 = {0.0, 2.0};
39 double[] start2 = {-1.0, 0.0};
40 double[] end2 = {1.0, 2.0};
42 double[] res = {0.0, 1.0};
44 PathElement e1 = new PathElement(start1, end1);
45 PathElement e2 = new PathElement(start2, end2);
47 double[] brm = BrmTronServer.findIntersection(e1, e2);
49 assertTrue(compareDouble(brm, res, dx) );
52 @Test
53 public void test2_findIntersection() {
54 final double dx = 1e-09;
56 double[] start1 = {0.1, 0.1};
57 double[] end1 = {0.0, 2.0};
58 double[] start2 = {-1.0, 0.2};
59 double[] end2 = {1.0, 200.0};
61 double[] res = null;
63 PathElement e1 = new PathElement(start1, end1);
64 PathElement e2 = new PathElement(start2, end2);
66 double[] brm = BrmTronServer.findIntersection(e1, e2);
68 assertTrue(compareDouble(brm, res, dx));
71 @Test
72 public void test1_checkForCollision() {
73 generateWalls(500);
74 double[] start = {0,1};
75 double [] end = {1,0};
76 PathElement p = new PathElement(start, end);
78 assertFalse(checkForCollision(p) == null);
80 p.setStart(500, 893);
81 p.setEnd(543, 489.1);
83 assertTrue(checkForCollision(p) == null);
86 private boolean compareDouble(double[] a, double[] b, double dx) {
87 if(a == null && b == null)
88 return true;
89 if((a == null && b != null) || (a != null && b ==null))
90 return false;
91 boolean ret=true;
92 for(int i=0; i<a.length; i++) {
93 if(Math.abs(a[i]-b[i]) > dx) {
94 ret = false;
95 break;
99 return ret;
102 private void generateWalls(int n) {
103 Random r = new Random();
104 double[] start = {0,0};
105 double[] end = {0,0};
106 while((n--)>0) {
107 start = end.clone();
108 end[0] = start[0] + r.nextDouble();
109 end[1] = start[1] + r.nextDouble();
110 PathElement p = new PathElement(start, end);
111 addWall(p);